package com.dong.c12_netty_websocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * <h1></h1>
 *
 * @author : dzj
 * @create: 2021-03-09 22:36
 */
public class WebSocketServer {
    public static void main(String[] args) {
        EventLoopGroup bossGroup = null;
        EventLoopGroup workerGroup = null;
        try {
            bossGroup = new NioEventLoopGroup(2);
            workerGroup = new NioEventLoopGroup(8);
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline()
                                .addLast(new HttpServerCodec())
                                    // 以块儿的方式写  添加ChunkedWriter
                                .addLast(new ChunkedWriteHandler())
                                    /**
                                     * 说明:
                                     * 1. http传输过程中是分段的,  HttpObjectAggregator 就是可以将多个段  聚合起来
                                     * 2. 这就是为什么当浏览器发送大量数据时, 就会发生多次http请求
                                     */
                                .addLast(new HttpObjectAggregator(8192))
                                    /**
                                     * 说明:  101
                                     * 1. 对于webSocket ,它的数据 以 frame, 传递的形式
                                     * 2. 可以看到, {@link WebSocketFrame} 下面有6个子类
                                     * 3. 浏览器发送请求时, ws://localhost:7000/hello
                                     * 4. {@link WebSocketServerProtocolHandler}   核心功能是将http协议升级为ws协议, 保持长连接
                                     */
                                .addLast(new WebSocketServerProtocolHandler("/hello"))


                                .addLast(new WebSocketServerHandler())

                            ;
                        }
                    });
            ChannelFuture channelFuture = serverBootstrap.bind(7000).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }
}
